
// <copyright file="DynamicToDataSetHelperExtensions.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The dynamic to data set helper extensions.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Data.Helpers
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Reflection;

    /// <summary>
    /// The dynamic to data set helper extensions.
    /// </summary>
    public static class DynamicToDataSetHelperExtensions
    {
        #region Public Methods and Operators

        /// <summary>
        /// Exports enumerable as DataTable and adds it to the given DataSet.
        /// </summary>
        /// <typeparam name="T">
        /// </typeparam>
        /// <param name="enumerable">
        /// The enumerable.
        /// </param>
        /// <param name="dataSet">
        /// The data set.
        /// </param>
        /// <param name="dataTableName">
        /// Name of the data table.
        /// </param>
        public static void AddToDataSet<T>(IEnumerable<T> enumerable, DataSet dataSet, string dataTableName) where T : class
        {
            DataTable table = DynamicToDataReaderDataTableHelper<T>.ExportAsDataTable(enumerable);
            table.TableName = dataTableName;
            dataSet.Tables.Add(table);
        }

        /// <summary>
        /// The export as data set.
        /// </summary>
        /// <param name="obj">
        /// The obj.
        /// </param>
        /// <typeparam name="T">
        /// </typeparam>
        /// <returns>
        /// The <see cref="DataSet"/>.
        /// </returns>
        public static DataSet ExportAsDataSet<T>(T obj)
        {
            return DynamicToDataSetHelper<T>.ExportAsDataSet(obj);
        }

        /// <summary>
        /// The export as data set.
        /// </summary>
        /// <param name="obj">
        /// The obj.
        /// </param>
        /// <returns>
        /// The <see cref="DataSet"/>.
        /// </returns>
        public static DataSet ExportAsDataSet(object obj)
        {
            Type toDataSetType = typeof(DynamicToDataSetHelper<>).MakeGenericType(obj.GetType());
            MethodInfo mi = toDataSetType.GetMethod("ExportAsDataSet");
            var dataSet = mi.Invoke(null, new[] { obj }) as DataSet;
            return dataSet;
        }

        #endregion Public Methods and Operators
    }
}